﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;
using System.Web;
using System.Web.Mvc;
using System.ServiceModel.Syndication;
using LevenBlog.Core.Data;
using LevenBlog.Core.Entities;
using LevenBlog.Core.Utils;
using LevenBlog.Core.Service;

namespace LevenBlog.Core.Mvc
{
    public class TrackXSLResult : ActionResult
    {
        public string Path { get; private set; }
        public IDbProvider Provider { get; set; }

        public TrackXSLResult(string path)
        {
            Path = path;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            //UrlHelper url = new UrlHelper(new ViewContext(context, "traxkxsl", string.Empty, (context.Controller as Controller).ViewData, (context.Controller as Controller).TempData));
            //UrlHelper url = new UrlHelper(new ViewContext(context, "trackxsl", context.Controller.ViewData, context.Controller.TempData));
            UrlHelper url = (context.Controller as Controller).Url;
            string fullPath = url.Content("/skins/" + Config.Get(Provider).skinpath + Path);
            fullPath = context.HttpContext.Server.MapPath(fullPath);
            if (!System.IO.File.Exists(fullPath))
                throw new FileNotFoundException(Path);
            FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            StreamReader sr = new StreamReader(fs, Encoding.UTF8);
            StringBuilder sb = new StringBuilder(sr.ReadToEnd());
            sr.Close();
            sb.Replace("{$skinpath}", url.Content("skins/" + Config.Get(Provider).skinpath));
            context.HttpContext.Response.ContentType = "text/xml;charset=utf-8";
            context.HttpContext.Response.Write(sb.ToString());
        }
    }
}
